package lanou3g.myfirstapp36kr.fragment;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.youth.banner.Banner;
import com.youth.banner.Banner.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import lanou3g.myfirstapp36kr.StringUrl;
import lanou3g.myfirstapp36kr.activity.CreateActivity;
import lanou3g.myfirstapp36kr.activity.CreateCompnayActivity;
import lanou3g.myfirstapp36kr.activity.FindgoodprojectActivity;
import lanou3g.myfirstapp36kr.activity.FragmentMoneyviewpagerFragmentLvDetailsActivity;
import lanou3g.myfirstapp36kr.activity.IammoneyActivity;
import lanou3g.myfirstapp36kr.activity.Learn36krActivity;
import lanou3g.myfirstapp36kr.activity.FindLookForMoneyActivity;
import lanou3g.myfirstapp36kr.activity.MoneyQuickActivity;
import lanou3g.myfirstapp36kr.activity.NearwFutureActivityActivity;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.activity.TurnPicDetailsActivity;
import lanou3g.myfirstapp36kr.adapter.FragmentFindListviewFirstAdapter;
import lanou3g.myfirstapp36kr.base.BaseFragment;
import lanou3g.myfirstapp36kr.ChangeButtonListener;
import lanou3g.myfirstapp36kr.entity.FragmentMoneyViewPagerFragmentListViewEntity;
import lanou3g.myfirstapp36kr.entity.FragmentNewsTurnPic;
import lanou3g.myfirstapp36kr.myvolleysingleton.VolleySingleton;

/**
 * Created by dllo on 16/8/2.
 */
public class FindFragment extends BaseFragment implements OnClickListener {
    private Banner banner;
    private ListView listView;
    private FragmentFindListviewFirstAdapter adapter;

    //ActivityLookforMoneyEntity entity;
    public static List<String> strUrl = new ArrayList<>();
    FragmentNewsTurnPic responseTurnPic;
    RelativeLayout relativeLayout;
    private ImageView imageViewKr;
    private FragmentManager manager;

    FragmentMoneyViewPagerFragmentListViewEntity response;

    @Override
    protected int getLayout() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView(View view) {
        relativeLayout = (RelativeLayout) view.findViewById(R.id.relative_layout_boss);
        relativeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(), CreateActivity.class);
                startActivity(intent);
            }
        });
        imageViewKr = (ImageView) view.findViewById(R.id.iv_36kr);
        imageViewKr.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),Learn36krActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.iv_company).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(), CreateCompnayActivity.class);
                startActivity(intent);
            }
        });
//        manager = getFragmentManager();
//        ft = manager.beginTransaction();
        //一个布局可以给两个Activity用,这么写控制不了MainActivity里的Button
//        View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main,null);
        //final Button buttonFind = (Button) view1.findViewById(R.id.btn_find);
//        final Button buttonMoney = (Button) view1.findViewById(R.id.btn_money);
//
//        final Button button_news = (Button) view1.findViewById(R.id.btn_news);
//
//        final Button button_message = (Button) view1.findViewById(R.id.btn_message);
//        final Button button_my = (Button) view1.findViewById(R.id.btn_my);
//热门项目按钮
        view.findViewById(R.id.relative_layout_hotproject_more).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//                ft.replace(R.id.framlayout_replace,new FragmentMoney());
//                ft.commit();
                ChangeButtonListener ChangeButtonListener = (ChangeButtonListener) getActivity();
                ChangeButtonListener.changeButton();

            }
        });
        //投资速递按钮
        view.findViewById(R.id.relaytive_layout_moneyquick).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),MoneyQuickActivity.class);
                startActivity(intent);

            }
        });
        //行业新闻
        view.findViewById(R.id.iv_news2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),MoneyQuickActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.btn_app).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),MoneyQuickActivity.class);
                startActivity(intent);
            }
        });
        view.findViewById(R.id.btn_activity).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),MoneyQuickActivity.class);
                startActivity(intent);
            }
        });
        //发现好项目
        view.findViewById(R.id.btn_find_good_app).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),FindgoodprojectActivity.class);
                startActivity(intent);

            }
        });
        //我是投资人
        view.findViewById(R.id.relateve_layout_iammoney).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FindFragment.this.getActivity(),IammoneyActivity.class);
                startActivity(intent);

            }
        });



        banner = (Banner) view.findViewById(R.id.view_pager_find);
        listView = (ListView) view.findViewById(R.id.list_view_hot_find);
        //寻找投资人按钮
        view.findViewById(R.id.btn_look_for).setOnClickListener(this);
        //近期活动
        view.findViewById(R.id.iv_activity).setOnClickListener(this);

    }

    @Override
    protected void initData() {
        //轮播图
        banner.setDelayTime(3000);
        banner.setImages(Fragmentnews.strUrl);
        adapter = new FragmentFindListviewFirstAdapter(getActivity());
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(View view, int position) {
                Intent intent = new Intent(FindFragment.this.getActivity(), TurnPicDetailsActivity.class);
                String urlNum = responseTurnPic.getData().getPics().get(position - 1).getLocation();
                intent.putExtra("urlNum",urlNum);
                startActivity(intent);

            }
        });
        adapter = new FragmentFindListviewFirstAdapter(getActivity());

        //拉取数据
        VolleySingleton.addRequest(StringUrl.stringFragmentMoneyLv, FragmentMoneyViewPagerFragmentListViewEntity.class,
                new Response.Listener<FragmentMoneyViewPagerFragmentListViewEntity>() {
            @Override
            public void onResponse(FragmentMoneyViewPagerFragmentListViewEntity response) {

                adapter.setEntity(response);
                listView.setAdapter(adapter);
                FindFragment.this.response = response;

            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.getMessage();
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(FindFragment.this.getActivity(), FragmentMoneyviewpagerFragmentLvDetailsActivity.class);
                String stringUrl = StringUrl.stringDetailsUrl + response.getData().getData().get(i).getCrowdFundingId();
                intent.putExtra("FragmentMoneyviewPagerFragmentUrl",stringUrl);
                startActivity(intent);
            }
        });

//        StringRequest stringRequestLookforMoney = new StringRequest("https://rong.36kr.com/api/mobi/investor?page=1&pageSize=20", new Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                 entity = gson.fromJson(response,ActivityLookforMoneyEntity.class);
//
//
//
//            }
//        }, new ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });

        //requestQueue.add(stringRequestLookforMoney);

        VolleySingleton.addRequest(StringUrl.stringNewsTurnPic, FragmentNewsTurnPic.class, new Response.Listener<FragmentNewsTurnPic>() {
            @Override
            public void onResponse(FragmentNewsTurnPic response) {
                for (int i = 0; i < response.getData().getPics().size(); i++) {
                    strUrl.add(response.getData().getPics().get(i).getImgUrl());
                    FindFragment.this.responseTurnPic = response;

                }

            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_look_for:
                Intent intent = new Intent(getActivity(), FindLookForMoneyActivity.class);
                //intent.putExtra("entity",entity);
                startActivity(intent);
                break;
            case R.id.iv_activity:
                Intent intent1 = new Intent(getActivity(), NearwFutureActivityActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
